package com.yuanzheng;

import com.alibaba.fastjson.JSONObject;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.Transaction;

/**
 * @author yuanzheng
 * @date 2020/7/5-15:49
 */
public class TestTransactions {
    public static void main(String[] args) {
        Jedis jedis = new Jedis("127.0.0.1", 6379);

        JSONObject js = new JSONObject();
        js.put("name", "Tom");
        js.put("age", 5);
        String user = js.toJSONString();

        Transaction multi = jedis.multi();
        try {
            multi.set("user1", user);
            multi.set("user2", user);
            multi.exec();
        } catch (Exception e) {
            multi.discard();
            e.printStackTrace();
        } finally {
            System.out.println(jedis.get("user2"));
            jedis.close();
        }
    }
}
